package own.stu.jobgib.playown.vediodown.bili.model.media;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.springframework.util.CollectionUtils;
import own.stu.jobgib.playown.vediodown.common.BaseModel;
import own.stu.jobgib.playown.vediodown.common.model.BaseDataInfo;
import own.stu.jobgib.playown.vediodown.common.model.ItemFilter;
import own.stu.jobgib.playown.vediodown.common.model.SimpleMediaItem;

import java.util.List;
import java.util.function.Function;

@lombok.Data
public class MediaData extends BaseModel<ListRecord> implements BaseDataInfo<MediaListItem, Void> {

    static String urlFormat = "https://api.bilibili.com/x/v2/medialist/resource/list?mobi_app=web&type=1&" +
            "biz_id=%d&otype=2&ps=%d&desc=false";

    public static String getUrl(Long mid) {
        return getUrl(mid, 1000);
    }

    private static boolean bvIdStartFlag = false;

    public static String getUrl(Long mid, int pageSize) {
        return String.format(urlFormat, mid, pageSize);
    }

    private static ItemFilter<MediaListItem> itemFilter;

    @Override
    public List<MediaListItem> getItemList() {
        return this.getData().getMediaList();
    }

    @Override
    public SimpleMediaItem transFor(MediaListItem mediaListItem) {
        SimpleMediaItem item = new SimpleMediaItem();
        BeanUtil.copyProperties(mediaListItem, item, CopyOptions.create().setIgnoreProperties("id"));
        return item;
    }

    @Override
    public Long getUserId() {
        List<MediaListItem> mediaList = this.getData().getMediaList();
        if (CollectionUtils.isEmpty(mediaList)) {
            return 0L;
        }
        return mediaList.get(0).getUpper().getMid();
    }

    @Override
    public ItemFilter<MediaListItem> getItemFilter() {
        return itemFilter;
    }

    public static void setItemFilter(Function<MediaListItem, Boolean> filter) {
        itemFilter = new ItemFilter<>(filter);
    }
}
